﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using SinaWeiboPlugin;
using SinaWeiboPlugin.Properties;
using Slippor.Douban.Radio.Assist;
using System.Web;

namespace SinaWeiboUserControl
{
    public partial class UploadControl : UserControl
    {
        private Settings _settings = (Settings)SinaWeiboConfig.SettingManager.Settings;

        private TweetBuilder _tweetBuilder;
        internal TweetBuilder Builder
        {
            set { _tweetBuilder = value; }
        }
        
        public UploadControl()
        {
            InitializeComponent();
            checkBoxWithSnap.Checked = _settings.WithSnapShot;
            checkBoxCover.Checked = _settings.SnapRegion == "Cover";
            SetSnapRegion();
        }

        private void SetSnapRegion()
        {
            if(_settings.SnapRegion == "Cover")
            {
                pictureBoxTweet.Width = 120;
                pictureBoxTweet.Height = 120;
            }
            else
            {
                pictureBoxTweet.Width = 125;
                pictureBoxTweet.Height = 60;
            }
        }

        public void UpdateInfo()
        {
            _tweetBuilder.Refresh();
            textBoxTweet.Text = _tweetBuilder.Message;
            pictureBoxTweet.Image = _tweetBuilder.Snap;
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textBoxTweet.Text))
            {
                SendTweet(textBoxTweet.Text);
            }
            
        }

        public void SendTweet(String message)
        {
            using (BackgroundWorker sendWorker = new BackgroundWorker())
            {
                buttonSend.Text = "发送中...";
                buttonSend.Enabled = false;
                _tweetBuilder.Message = message;
                sendWorker.DoWork += (senderD, eD) =>
                {
                    eD.Result = _tweetBuilder.Upload();
                };
                sendWorker.RunWorkerCompleted += (senderC, eC) =>
                {
                    if (eC.Error == null && (bool)eC.Result)
                    {
                        buttonSend.Text = "发送成功";
                    }
                    else
                    {
                        buttonSend.Text = "发送失败";
                        if (_settings.ErrorAlert)
                        {
                            if (eC.Error != null)
                            {
                                MessageBox.Show(eC.Error.Message);
                            }
                        }
                    }
                    buttonSend.Enabled = true;
                };
                sendWorker.RunWorkerAsync();
            }
        }

        private void textBoxTweet_TextChanged(object sender, EventArgs e)
        {
            labelLength.Text = textBoxTweet.Text.Length.ToString();
            if(textBoxTweet.Text.Length > 140)
            {
                buttonSend.Enabled = false;
            }
            else
            {
                buttonSend.Enabled = true;
            }
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            UpdateInfo();
        }

        private void checkBoxWithSnap_CheckStateChanged(object sender, EventArgs e)
        {
            if(checkBoxWithSnap.Checked != _settings.WithSnapShot)
            {
                _settings.WithSnapShot = checkBoxWithSnap.Checked;
                _settings.Save();
            }
        }

        private void checkBoxCover_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxCover.Checked != (_settings.SnapRegion == "Cover"))
            {
                _settings.SnapRegion = (checkBoxCover.Checked ? "Cover" : String.Empty);
                _settings.Save();
                SetSnapRegion();
                UpdateInfo();
            }
        }

        bool _zoom = false;
        private void pictureBoxTweet_Click(object sender, EventArgs e)
        {
            _zoom = !_zoom;
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            RadioUtility.Browse("http://t.sina.com.cn/");
        }

        private void linkLabelSearch_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RadioUtility.Browse(String.Format("http://t.sina.com.cn/k/{0}", HttpUtility.UrlEncode("豆瓣电台")));
        }
    }
}
